package com.sinya.shoppingmall.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinya.shoppingmall.MyAddressActivity;
import com.sinya.shoppingmall.MyAssistantActivity;
import com.sinya.shoppingmall.MyOrderListActivity;
import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.bean.Constants;
import com.sinya.shoppingmall.utils.FileHelper;
import com.sinya.shoppingmall.utils.ImageLoaderUtils;
import com.sinya.shoppingmall.utils.IntoUtils;
import com.sinya.shoppingmall.widget.pulllayout.PullLayout;

import java.io.File;

/**
 * 我的
 *
 * @author KoizumiSinya
 * @date 2015/12/25. 0:13
 */

public class MineFragment extends BaseFragment {

    private PullLayout pullLayout;
    private RelativeLayout rl_orderdetails;
    private LinearLayout ll_order_cancel;
    private TextView iv_help;
    private TextView tv_address;

    private Bitmap head;//头像Bitmap
    private ImageView my_icon;

    @Override
    protected View initView() {
        return View.inflate(mContext, R.layout.fragment_five, null);
    }

    @Override
    protected void setView() {
        initHeaderViews();
        initOrderViews();
        initOtherViews();
    }

    /**
     * 实例化 顶部隐藏布局中的控件
     */
    private void initHeaderViews() {

        //设置首次进来默认开启 PullLayout
        pullLayout = (PullLayout) rootView.findViewById(R.id.pull_layout);
        pullLayout.open();

        //头像
        my_icon = (ImageView) rootView.findViewById(R.id.my_icon);
        my_icon.setOnClickListener(onClick);
        Bitmap bt = BitmapFactory.decodeFile(FileHelper.getSDPath() + Constants.USER_PICTURE_NAME);//从Sd中找头像，转换成Bitmap
        if (bt != null) {
            Drawable drawable = new BitmapDrawable(bt);//转换成drawable
            my_icon.setImageDrawable(drawable);
        } else {
            //TODO 如果SD里面没有则需要从服务器取头像，取回来的头像再保存在SD中
        }

        //TODO 其它几个 收藏店铺、会员等控件 同理在此设置

    }

    private void initOrderViews() {

        //订单部分
        rl_orderdetails = (RelativeLayout) rootView.findViewById(R.id.rl_orderdetails);
        rl_orderdetails.setOnClickListener(onClick);

        ll_order_cancel = (LinearLayout) rootView.findViewById(R.id.ll_order_cancel);
        ll_order_cancel.setOnClickListener(onClick);

        iv_help = (TextView) rootView.findViewById(R.id.iv_help);
        iv_help.setOnClickListener(onClick);
    }


    private void initOtherViews() {
        tv_address = (TextView) rootView.findViewById(R.id.tv_address);
        tv_address.setOnClickListener(onClick);
    }


    // [+] click

    View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle;
            switch (v.getId()) {

                case R.id.my_icon:
                    //TODO 点击头像
                    Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                    intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent1, 1);

                    break;

                case R.id.rl_orderdetails:
                    //TODO 进入订单列表
                    bundle = new Bundle();
                    bundle.putInt("OrderType", 2);
                    IntoUtils.goActivity(mContext, Intent.FLAG_ACTIVITY_NEW_TASK, bundle, MyOrderListActivity.class);
                    break;

                case R.id.ll_order_cancel:
                    //TODO 取消订单
                    bundle = new Bundle();
                    bundle.putInt("OrderType", 3);
                    IntoUtils.goActivity(mContext, Intent.FLAG_ACTIVITY_NEW_TASK, bundle, MyOrderListActivity.class);
                    break;

                case R.id.iv_help:
                    //TODO 跳转到帮助中心

                    break;
                case R.id.tv_address:
                    //TODO 跳转到地址管理
                    IntoUtils.goActivity(mContext, Intent.FLAG_ACTIVITY_NEW_TASK, MyAddressActivity.class);
                    break;

                default:
                    break;
            }
        }
    };

    // [-] click

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case 1:
                //从拍照中返回
                if (resultCode == Activity.RESULT_OK) {
                    //裁剪图片
                    cropPhoto(data.getData());
                }

                break;
            case 2:
                //从相册中返回
                if (resultCode == Activity.RESULT_OK) {
                    File temp = new File(FileHelper.getSDPath() + Constants.USER_PICTURE_NAME);
                    //裁剪图片
                    cropPhoto(Uri.fromFile(temp));
                }

                break;
            case 3:
                //剪裁图片确认后返回
                if (data != null) {
                    Bundle extras = data.getExtras();
                    head = extras.getParcelable("data");
                    if (head != null) {
                        //TODO 这里可以添加上传服务器的代码


                        //TODO 接着保存在SD卡中
                        FileHelper.saveBitmapInSD(head, FileHelper.getSDPath(), Constants.USER_PICTURE_NAME);
                        //TODO 更换图片
                        my_icon.setImageBitmap(ImageLoaderUtils.getCirlceImage(head));
                    }
                    break;
                }
            default:
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 调用系统的裁剪
     *
     * @param uri
     */
    public void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 480);
        intent.putExtra("outputY", 480);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 3);
    }


}
